package index

import "sync/atomic"

type LB struct {
	target []string

	uint64
}

func NewLB(target []string) *LB {
	return &LB{target: target}
}

// 实现基本轮寻负载均衡
func (lb *LB) Get() string {
	var old uint64
	for {
		old = atomic.LoadUint64(&lb.uint64)
		if atomic.CompareAndSwapUint64(&lb.uint64, old, old+1) {
			break
		}
	}
	return lb.target[old%uint64(len(lb.target))]
}
